import { _decorator, Component, Node, NodePool, Prefab, instantiate } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('SmartNodePool')
export class SmartNodePool extends NodePool {

    prefab: Prefab = null;

    setPrefab(prefab: Prefab) {
        this.prefab = prefab;
    }

    prepare(count: number) {
        if (this.prefab == null) return;
        for (let i = 0; i < count; i++) {
            if (this.size() >= count) break;
            this.putNode(instantiate(this.prefab));
        }
    }

    getNode(): Node {
        if (this.size() > 0) return this.get();
        return instantiate(this.prefab);
    }

    putNode(node: Node) {
        return this.put(node);
    }

    clear() {
        super.clear();
    }
}


